package com.konsung.ksdc.manager.advice;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.SuggestionNetBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/3/18 0018.
 */
public class AdviceAdapter extends BaseAdapter {
    private List<SuggestionNetBean> mDatas;

    /**
     *
     * @param suggestionNetBeanListList 数据bean
     */
    public AdviceAdapter(List<SuggestionNetBean> suggestionNetBeanListList) {
        mDatas = suggestionNetBeanListList;
    }

    @Override
    public int getCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        SuggestionNetBean bean = mDatas.get(position);

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(MyApplication.getContext())
                    .inflate(R.layout.item_listview_advice, null);
            ButterKnife.bind(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String createDate = bean.getCreateDate();
        int i = createDate.indexOf("&");
        String date = createDate.substring(0, i);
        holder.tvCreateDate.setText(date);

        holder.tvSuggestionContent.setText(parseContent(bean.getSuggestionContent()));
        holder.tvSuggestionState.setText(parseState(bean.getSuggestionState(), holder));
        return convertView;
    }

    /**
     * 处理内容超长问题
     * @param suggestionContent 内容
     * @return 内容
     */
    private String parseContent(String suggestionContent) {
        if (StringUtils.isEmpty(suggestionContent)) {
            return "";
        }

        if (suggestionContent.length() > 42) {
            return suggestionContent.subSequence(0, 42) + "...";
        }

        return suggestionContent;
    }

    /**
     * 转换回复状态
     * @param stateCode 状态code
     * @param holder 对象
     * @return 字符串
     */
    private String parseState(String stateCode, ViewHolder holder) {
        int color = MyApplication.getContext().getResources().getColor(R.color.text_normal);

        if (SuggestionNetBean.STATE_NO_REPLIED.equals(stateCode)) {
            holder.tvSuggestionState.setTextColor(color);
            return MyApplication.getContext().getString(R.string.state_no_reply);
        }

        color = MyApplication.getContext().getResources().getColor(R.color.title_text_bg_pressed);
        holder.tvSuggestionState.setTextColor(color);
        return MyApplication.getContext().getString(R.string.state_replied);
    }

    /**
     * 内部类
     */
    static class ViewHolder {
        @Bind(R.id.tv_create_date)
        TextView tvCreateDate;
        @Bind(R.id.tv_suggestion_content)
        TextView tvSuggestionContent;
        @Bind(R.id.tv_suggestion_state)
        TextView tvSuggestionState;
    }

    /**
     * 设置更新数据
     * @param list 售后集合list
     */
    public void refreshData(List<SuggestionNetBean> list) {

        mDatas.clear();
        mDatas = list;
        notifyDataSetChanged();
    }
}
